package E2013;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class e10 extends JFrame implements ActionListener {


    JTextArea textArea = new JTextArea(3,20);
    JTextField textField = new JTextField(10);
    JButton button = new JButton("清空");

    public void showGUI() {
        // 初始化设置
        this.setSize(500,300);
        this.setLocation(500,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        // 将监听器注册到事件源
        button.addActionListener(this);
        textField.addActionListener(this);
        //添加组件
        this.add(textField);
        this.add(textArea);
        this.add(button);
    }

    public static void main(String[] args) {
        e10 e = new e10();
        e.showGUI();
    }

    @Override
    //ActionEvent e 参数e表示触发的事件源
    public void actionPerformed(ActionEvent e) {
        // 判断事件来源
        if (e.getSource() == button){
            // 处理按钮清空效果
            textField.setText("");
            textArea.setText("");
        }
        if(e.getSource() == textField){
            String str = textField.getText();
            textArea.setText(str+"\n"+str+"\n"+str);
        }
    }
}
